package com.example.fang.demodruid.config;


import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Log4jFilter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletConfig;
import javax.sql.DataSource;
import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//@Configuration
public class DataSourceConfig {

   /**
    * @Author 01410843
    * @Description 将配置移到配置文件
    * @Date 10:52 2022/6/1
    * @Param []
    * @return javax.sql.DataSource
    **/
  /* @ConfigurationProperties("spring.datasource")
   @Bean*/
   public DataSource dataSource() throws SQLException {
      DruidDataSource druidDataSource=new DruidDataSource();
       druidDataSource.setMaxActive(1000);
       druidDataSource.setInitialSize(1);
       druidDataSource.setMaxWait(60000);
       druidDataSource.setMinIdle(1);
       druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
       druidDataSource.setMinEvictableIdleTimeMillis(300000);
       druidDataSource.setTestWhileIdle(true);
       druidDataSource.setTestOnBorrow(false);
       druidDataSource.setTestOnReturn(false);
      druidDataSource.setValidationQuery("select 1");
      //设置将监控文件刷新到日志
      druidDataSource.setTimeBetweenLogStatsMillis(3000000);
      Slf4jLogFilter slf4jLogFilter=new Slf4jLogFilter();
      //2、开启druid防火墙功能过滤器
      WallFilter wallFilter=new WallFilter();
      WallConfig wallConfig=new WallConfig();
      //multiStatementAllow
      wallConfig.setDeleteWhereNoneCheck(true);
      wallFilter.setConfig(wallConfig);
      wallFilter.getConfig().setDeleteWhereNoneCheck(true);//开启禁止删除整个表数据
      List<Filter> filters=new ArrayList<>();
      //sql监控过滤器
      StatFilter statFilter=new StatFilter();
      statFilter.setMergeSql(true);    //开启StatFilter合并统计sql功能
      statFilter.setSlowSqlMillis(2000);
      //慢SQL记录记录到日志
      statFilter.setLogSlowSql(true);
      //将拦截器放入拦截器链中
     // filters.add(slf4jLogFilter);
      filters.add(wallFilter);
      filters.add(statFilter);
      druidDataSource.setProxyFilters(filters);
      return  druidDataSource;
   }

   /**
    * 容器中放入一个servlet 配置druid的内置监控页面
    * @return
    */
   @Bean
   public ServletRegistrationBean servletRegistrationBean(){
      StatViewServlet statViewServlet=new StatViewServlet();
      ServletRegistrationBean servletRegistrationBean=new   ServletRegistrationBean(statViewServlet,"/druid/*");
      //配置监控页面的账号密码
      servletRegistrationBean.addInitParameter("loginUsername","admin");
      servletRegistrationBean.addInitParameter("loginPassword","admin");
      //在StatViewSerlvet输出的html页面中，有一个功能是Reset All，执行这个操作之后，会导致所有计数器清零，重新计数。你可以通过配置参数关闭它
      servletRegistrationBean.addInitParameter("resetEnable","false");
      return  servletRegistrationBean;
   };

   /**
    * 容器中放入一个过滤器Fileter
    * @return
    */
   @Bean
   public FilterRegistrationBean registrationBean(){
      WebStatFilter webStatFilter=new WebStatFilter();
      FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(webStatFilter);
      filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
      filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/");
      return filterRegistrationBean;

   };
}
